package com.quantstack.llcommonlibrary.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextPaint;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.File;

/**
 * 完成适配器ViewHolder
 */
public class CommonViewHolder extends RecyclerView.ViewHolder {

    //子View集合
    private SparseArray<View> mViews;
    private View mContentView;

    public CommonViewHolder(@NonNull View itemView) {
        super(itemView);
        mViews = new SparseArray();
        mContentView = itemView;
    }

    /**
     * getViewHolder
     * @param viewGroup
     * @param layoutId
     * @return
     */
    public static CommonViewHolder getViewHolder(ViewGroup viewGroup, int layoutId){
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutId,viewGroup,false);
        //View view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutId,null);
        return new CommonViewHolder(view);
    }

    /**
     * 提供外部访问View
     */
    public <T extends View> T getView(int viewId){
        View view = mViews.get(viewId);
        if(view==null){
            view = mContentView.findViewById(viewId);
            mViews.put(viewId,view);
        }
        return (T)view;
    }

    /**
     * 设置文本
     *
     * @param viewId
     * @param text
     * @return
     */
    public CommonViewHolder setText(int viewId, String text) {
        TextView tv = getView(viewId);
        tv.setText(text);
        return this;
    }

    /**
     * 设置图片链接
     *
     * @param mContext
     * @param viewId
     * @param url
     * @return
     */
    public CommonViewHolder setImageUrl(Context mContext, int viewId, String url) {
        ImageView iv = getView(viewId);
        //GlideHelper.loadUrl(mContext, url, iv);
        return this;
    }

    /**
     * 压缩
     *
     * @param mContext
     * @param viewId
     * @param url
     * @param w
     * @param h
     * @return
     */
    public CommonViewHolder setImageUrl(Context mContext, int viewId, String url, int w, int h) {
        ImageView iv = getView(viewId);
        //GlideHelper.loadSmollUrl(mContext, url, w, h, iv);
        return this;
    }


    /**
     * 设置图片文件
     *
     * @param mContext
     * @param viewId
     * @param file
     * @return
     */
    public CommonViewHolder setImageFile(Context mContext, int viewId, File file) {
        ImageView iv = getView(viewId);
        //GlideHelper.loadFile(mContext, file, iv);
        return this;
    }

    /**
     * 设置图片
     *
     * @param viewId
     * @param resId
     * @return
     */
    public CommonViewHolder setImageResource(int viewId, int resId) {
        ImageView iv = getView(viewId);
        iv.setImageResource(resId);
        return this;
    }

    /**
     * 设置背景颜色
     *
     * @param viewId
     * @param color
     * @return
     */
    public CommonViewHolder setBackgroundColor(int viewId, int color) {
        ImageView iv = getView(viewId);
        iv.setBackgroundColor(color);
        return this;
    }

    /**
     * 设置文本颜色
     *
     * @param viewId
     * @param color
     * @return
     */
    public CommonViewHolder setTextColor(int viewId, int color) {
        TextView tv = getView(viewId);
        tv.setTextColor(color);
        return this;
    }

    /**
     * 设置文本大小 加粗
     *
     * @param viewId
     * @param
     * @return
     */
    public CommonViewHolder setTextSizeAndColor(int viewId, int size) {
        TextView tv = getView(viewId);
        tv.setTextColor(Color.parseColor("#666666"));
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,size);
        //tv.setTextSize(size);
        TextPaint tp = tv.getPaint();
        tp.setFakeBoldText(true);
        return this;
    }


    /**
     * 设置控件的显示隐藏
     *
     * @param viewId
     * @param visibility
     * @return
     */
    public CommonViewHolder setVisibility(int viewId, int visibility) {
        TextView tv = getView(viewId);
        tv.setVisibility(visibility);
        return this;
    }
}
